package com.arpa.ntocc.common.security.filter;

import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.LogUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 统一日志过滤器
 * @Author liuyj
 * @Date 2019-11-18 19:06:51
 */
@Log4j2
@WebFilter(filterName = "LogFilter", urlPatterns = "/**")
@Configuration
public class LogFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            HttpServletRequest request = (HttpServletRequest)servletRequest;
            //排除OPTIONS预请求记录
            if(!"OPTIONS".equals(request.getMethod())){
                log.info(LogUtil.log());
            }
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

}
